package com.android.takeaway.fragment;

import android.content.res.Resources;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.takeaway.R;
import com.android.takeaway.adapter.ShopAdapter;
import com.android.takeaway.bean.Food;
import com.android.takeaway.bean.Shop;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Fragment 提供了一种模块化的方法，
 * 可以在不同的屏幕尺寸和方向上复用 UI 组件。
 */
public class ShopFragment extends Fragment {
    TextView tv_back, tv_title;
    RecyclerView recyclerView_shop;
    ShopAdapter adapter;
    View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_shop, container, false);
        initView();
        return view;
    }

    //初始化视图
    private void initView() {
        tv_back = view.findViewById(R.id.tv_back);
        tv_back.setVisibility(View.GONE); //隐藏返回按钮
        tv_title = view.findViewById(R.id.tv_title);
        tv_title.setText("店铺");
        recyclerView_shop = view.findViewById(R.id.recyclerView_shop);
        // 设置 RecyclerView 的布局管理器为线性布局
        recyclerView_shop.setLayoutManager(new LinearLayoutManager(getContext()));
        // 定义适配器，设置数据源，设置适配器
        adapter = new ShopAdapter(getContext());
        adapter.setData(parseJSONWithJSONObject(JSON2String())); // 从 JSON 数据中解析数据并设置适配器的数据源
        recyclerView_shop.setAdapter(adapter);// 设置 RecyclerView 的适配器
    }

    // 将jSON格式数据转换为字符串
    private String JSON2String() {
        String conversion = null;
        try {
            // 获取资源对象
            Resources res = getResources();
            // 打开JSON文件作为输入流
            InputStream inputStream = res.openRawResource(R.raw.takeaway);
            // 将输入流转换为字符串
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            inputStream.close();
            // 将字符串解析为JSON对象数组
            conversion = sb.toString();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        return conversion;
    }

    // jsonobject解析json的字符串格式数据
    private List<Shop> parseJSONWithJSONObject(String jsonString) {
        List<Shop> shopBeanList = new ArrayList<>();
        try {
            // 获取商店对象的JSON数组
            JSONArray shopArray = new JSONArray(jsonString);
            // 依次取出数组中的商店对象
            for (int i = 0; i < shopArray.length(); i++) {
                JSONObject shopObject = shopArray.getJSONObject(i);
                // 解析商店对象的属性
                int id = shopObject.getInt("id");
                String shopName = shopObject.getString("shopName");
                int saleNum = shopObject.getInt("saleNum");
                BigDecimal offerPrice = BigDecimal.valueOf(shopObject.getDouble("offerPrice"));
                BigDecimal distributionCost = BigDecimal.valueOf(shopObject.getDouble("distributionCost"));
                String welfare = shopObject.getString("welfare");
                String time = shopObject.getString("time");
                String shopPic = shopObject.getString("shopPic");
                String shopNotice = shopObject.getString("shopNotice");
                List<Food> foodBeanList = new ArrayList<>();
                // 获取商品对象的JSON数组
                JSONArray foodArray = shopObject.getJSONArray("foodList");
                // 依次取出数组中的商品对象
                for(int j = 0; j < foodArray.length(); j++) {
                    JSONObject foodObject = foodArray.getJSONObject(j);
                    int foodId = foodObject.getInt("foodId");
                    String foodName = foodObject.getString("foodName");
                    String taste = foodObject.getString("taste");
                    int foodNum = foodObject.getInt("saleNum");
                    BigDecimal price = BigDecimal.valueOf(foodObject.getDouble("price"));
                    int count = foodObject.getInt("count");
                    String foodPic = foodObject.getString("foodPic");
                    Food foodBean = new Food(foodId, foodName, taste, foodNum, price, count, foodPic);
                    foodBeanList.add(foodBean);
                }
                Shop shopBean = new Shop(id, shopName, saleNum, offerPrice, distributionCost, welfare, time, shopPic, shopNotice, foodBeanList);
                shopBeanList.add(shopBean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return shopBeanList;
    }
}